package com.szxy.hotel.controller;

import com.szxy.hotel.entity.Client;
import com.szxy.hotel.entity.Hotelroom;
import com.szxy.hotel.entity.PageBean;
import com.szxy.hotel.entity.Room;
import com.szxy.hotel.service.CheckService;
import com.szxy.hotel.service.ClientService;
import com.szxy.hotel.service.ReserveService;
import com.szxy.hotel.util.App;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CheckController {
    @Resource(name = "checkService")
    CheckService checkService;
    @Resource(name = "reserveService")
    ReserveService reserveService;
    @Resource(name = "clientService")
    ClientService clientService;

    /**
     * 确定登记
     */
    @RequestMapping("/addCheck.do")
    public String addCheck(Room room, Client client){
        Client c = clientService.findByclientname(client.getClientName());
        Hotelroom h = reserveService.findByhotelroomid(room.getRoomId());
        Double hrPrice = h.getHotelPrice();
        Room room1 = reserveService.findByroomid(room.getRoomId());
        if(room1 !=null&& room1.getRoomState().equals("已预订")||room1 !=null&&room1.getRoomState().equals("已入住")){
            return "redirect:toCheck.do";
        }else {
            if(c!=null&&c.getClientPhone().equals(client.getClientPhone())&&c.getClientIdentity().equals(client.getClientIdentity())){
                Double money = c.getClientMoney();
                if(room.getRoomIn()!=null&&room.getRoomOut()!=null){
                    String roomintime = room.getRoomIn();
                    String roomouttime = room.getRoomOut();
                    String[] intime = roomintime.split("-");
                    String[] outtime = roomouttime.split("-");
                    int inyear = Integer.parseInt(intime[0]);
                    int inmonth = Integer.parseInt(intime[1]);
                    int indate = Integer.parseInt(intime[2]);
                    int outyear = Integer.parseInt(outtime[0]);
                    int outmonth = Integer.parseInt(outtime[1]);
                    int outdate = Integer.parseInt(outtime[2]);
                    if(inyear<outyear){
                        if(inmonth==12&&outmonth==1){
                            if((outyear-inyear)==1){
                                int inalldate = App.day(inyear,inmonth);
                                Double m = (inalldate-indate+outdate)*hrPrice;
                                int d = inalldate-indate+outdate;
                                booleanMoney(money,client,room,m,c,hrPrice,d);
                            }
                        }
                    }else if(inyear==outyear){
                        if(inmonth<outmonth){
                            if((outmonth-inmonth)==1){
                                int inalldate = App.day(inyear,inmonth);
                                Double m = (inalldate-indate+outdate)*hrPrice;
                                int d = inalldate-indate+outdate;
                                booleanMoney(money,client,room,m,c,hrPrice,d);
                            }
                        }else if(inmonth==outmonth){
                            if(indate<outdate){
                                Double m = (outdate-indate)*hrPrice;
                                int d = outdate-indate;
                                booleanMoney(money,client,room,m,c,hrPrice,d);
                            }
                        }
                    }
                }
            }
        }
        return "redirect:toCheck.do";
    }

    /**
     * 确定登记的相关操作
     */
    private void booleanMoney(Double money, Client client, Room room, Double m, Client c, Double hrPrice, int d) {
        if(money>=client.getClientMoney()){
            if(client.getClientMoney()>=m){
                money = money-m;
                room.setRoomPrice(hrPrice);
                room.setRoomState("已入住");
                room.setRoomName(c.getClientName());
                room.setRoomTime(d);
                int result = reserveService.insertRoom(room);
                if(result==1){
                    clientService.updateClient(c.getClientName(),money);
                    reserveService.updateHotelstate(room.getRoomId(),room.getRoomState());
                }
            }
        }
    }
    /**
     * 取消登记
     */
    @RequestMapping("/cancelCheck.do")
    public String cancelCheck(Room room, Client client){
        Client c = clientService.findByclientname(client.getClientName());
        Room room1 = reserveService.findByroomid(room.getRoomId());
        if(c!=null&&c.getClientIdentity().equals(client.getClientIdentity())){
            if(room1 !=null&& room1.getRoomState().equals("已入住")&&c.getClientName().equals(room1.getRoomName())){
                String rs = "已退房";
                int result = reserveService.deleteReserve(room1.getRoomId(),rs);
                if(result==1){
                    Double m = room1.getRoomPrice()*room1.getRoomTime();
                    m += c.getClientMoney();
                    clientService.updateClient(c.getClientName(),m);
                    reserveService.updateHotelstate(room1.getRoomId(),rs);
                }
            }
        }
        return "redirect:toCheck.do";
    }
    /**
     * 房间入住列表
     */
    @RequestMapping("/checkList.do")
    @ResponseBody
    public Map<String,Object> checkList(int currentPage){
        Map<String, Object> map = new HashMap<String, Object>();
        PageBean<Room> pageBean = new PageBean<Room>();
        String roomtype = "已入住";
        int pageSize = 8;
        int start = (currentPage - 1) * pageSize;
        int size = pageSize;
        List<Room> lists = reserveService.findByPage(start, size,roomtype);
        List<Room> list = reserveService.selectCount(roomtype);
        int count = list.size();
        if (count <= 0) {
            map.put("status", 1);
            map.put("message", "没有数据");
            map.put("result", null);
        }else {
            int totalCount = count;
            double tc = totalCount;
            //总页数
            int totalPage = (int) Math.ceil(tc / pageSize);
            pageBean.setCurrentage(currentPage);
            pageBean.setPageSize(pageSize);
            pageBean.setTotalCount(totalCount);
            pageBean.setTotalPage(totalPage);
            pageBean.setLists(lists);
            map.put("status", 0);
            map.put("message", "有数据");
            map.put("result", pageBean);
        }
        return map;
    }
}
